package com.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.common.uilt.DateUtil;
import com.model.ReceiptBean;
import com.model.ReceiptDetailsBean;
import com.service.imp.AssetsService;
import com.service.imp.MeasureService;
import com.service.imp.ReceiptService;
import com.service.imp.UserService;

@Controller
@ParentPackage("defaultpackage")
@Namespace("/back")
@Action(value = "assetsPD", results = {
		@Result(name="browseentry",		location="/back/assetsMS/browse_entry.jsp"),
		@Result(name="inventory",		location="/back/assetsMS/inventory.jsp"),
		@Result(name="reportform",		location="/back/assetsMS/reportform.jsp"),
		@Result(name="dept_d_form",		location="/back/assetsMS/form/dept_d_form.jsp"),
		@Result(name="assetsShift",		location="/back/assetsMS/assetsShift.jsp"),
		@Result(name="dept_Three_form",	location="/back/assetsMS/form/dept_Three_form.jsp"),
		@Result(name="class_d_form",	location="/back/assetsMS/form/class_d_form.jsp"),
		@Result(name="receiptMS",		location="/back/assetsMS/receiptMS.jsp"),
		//@Result(name="all_assets_form",	location="/back/assetsMS/form/all_assets_form.jsp"),
		//@Result(name="all_d_form",		location="/back/assetsMS/form/all_d_form.jsp"),
		@Result(name = "error", 		location = "/error.jsp"), })
public class PDAction extends BackAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1715527583516474638L;
	@Autowired
	private ReceiptService receiptService;
	@Autowired
	private AssetsService assetsService;
	@Autowired
	private MeasureService measureServic;
	@Autowired
	private UserService userService;
	private List<ReceiptDetailsBean> receiptdetailslist;
	private ReceiptBean receiptBean;
	private static final Logger logger = Logger
			.getLogger(PDAction.class);
	private String data_month;
	/**
	 * 浏览录入界面
	 * @return
	 */
	
	public String browseentry(){
		//到这里
		logger.debug("应该有输出");
		try {
			request.setAttribute("date", new Date());
			//request.setAttribute("URL",request.getRequestURL());
			request.setAttribute("tableSetting", this.userService.getTableSetting(session, "tb_assets"));
			request.setAttribute("assetsStoreList", this.measureServic.list("存放地点"));
			request.setAttribute("assetsSourceList", this.measureServic.list("资产来源"));
			request.setAttribute("assetsMeasureList",this.measureServic.list("计量单位"));
			request.setAttribute("assetsStatusList",this.measureServic.list("资产状态"));
			this.assetsService.getAssetsBystatus(currentPage,sortField,orderType,maxrecord,request);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return "browseentry";
	}
	
	/**
	 * 盘点
	 */
	public String inventory(){
		try {
			//判断当前月份是否已经盘点
			if(this.receiptService.isMonthPD()){
				request.setAttribute("date", new Date());
				request.setAttribute("receipt_no", this.receiptService.getReceiptNO("PD"));
				request.setAttribute("inventoryList", this.assetsService.getAllassetsBygroup());
			}else{
				request.setAttribute("tip", "本月已经盘点");
				return reportform();
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return "inventory";
	}
	/**
	 * 报表
	 * @return
	 */
	public String reportform(){
		try {
			request.setAttribute("date", new Date());
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return "reportform";
	}
	
	/**
	 * 保存盘点单据
	 */
	public String sevaInventory(){
		try {
			Date currentDate= new Date();
			int month =	DateUtil.getInteger(currentDate, Calendar.MONTH);
			int day=DateUtil.getInteger(currentDate, Calendar.DAY_OF_MONTH);
			if(month==2){
				if(day<25){
					request.setAttribute("tip", "今天不可以盘点");
					return inventory();
				}
			}else{
				if(day<28){
					request.setAttribute("tip", "今天不可以盘点");
					return inventory();
				}
			}
			//盘点开始 先计提折旧
			this.assetsService.fold();	
			this.receiptService.inventory(this.receiptBean,this.receiptdetailslist);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return reportform();
	}
	public String assetsshift() throws Exception{
		return "assetsShift";
	}
	public String receiptMS() throws Exception{
		
		return "receiptMS";
	}
	/**
	 * 部门折旧报表
	 */
	public String report_dept_d_form() throws Exception{
		try {
			if(null==data_month||"".equals(data_month)){
				data_month=DateUtil.SD3.format(new Date());
			}
			request.setAttribute("formList", this.receiptService.getreceiptdept_dform(data_month));
			request.setAttribute("data_month", data_month);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return "dept_d_form";
	}
	/**
	 * 部门三栏账
	 * 
	 */
	public String report_dept_Three_form() throws Exception{
		try {
			if(null==data_month||"".equals(data_month)){
				data_month=DateUtil.SD3.format(new Date());
			}
			request.setAttribute("formList", 
					this.receiptService.getReceiptdeptThreeForm(this.data_month));
			request.setAttribute("data_month", data_month);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return "dept_Three_form";
	}
	
	/**
	 * 类别折旧报表
	 */
	public String report_class_d_form() throws Exception{
		try {
			if(null==data_month||"".equals(data_month)){
				data_month=DateUtil.SD3.format(new Date());
			}
			request.setAttribute("formList", this.receiptService.getreceiptcalss_dform(data_month));
			request.setAttribute("data_month", data_month);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("tip", "发生错误");
			return "error";
		}
		return "class_d_form";
	}
	public List<ReceiptDetailsBean> getReceiptdetailslist() {
		return receiptdetailslist;
	}
	public void setReceiptdetailslist(List<ReceiptDetailsBean> receiptdetailslist) {
		this.receiptdetailslist = receiptdetailslist;
	}
	public ReceiptBean getReceiptBean() {
		return receiptBean;
	}
	public void setReceiptBean(ReceiptBean receiptBean) {
		this.receiptBean = receiptBean;
	}

	public String getData_month() {
		return data_month;
	}

	public void setData_month(String data_month) {
		this.data_month = data_month;
	}
}
